package com.andnnl.xiaoshuo;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.andnnl.utils.HttpHelper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by chenss on 2022/2/14.
 */
public class XiaoShuoDownTest {
    public static final int GET_CODE_TIMES = 3;//获取验证码最多5次
    public static final int SEND_TIMES = 19;//发送最多3次
    public static final int CONNECTION_TIMEOUT = 10000;
    public static final int READ_TIMEOUT = 10000;

    public static void main(String[] args) throws IOException {
        String userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";
        HttpResponse rs;//下载验证码图片
        String urlString = "https://www.22mt.com/wandaoshendilichenyuan/";
        HttpRequest request = HttpUtil.createGet(urlString);
//        request.cookie(cookiesStr);
        request.header(Header.USER_AGENT, userAgent)
                .header(Header.REFERER, urlString)
                .setConnectionTimeout(CONNECTION_TIMEOUT)
                .setReadTimeout(READ_TIMEOUT)
                .setFollowRedirects(true);
        rs = request.execute();//执行下载
        String content = rs.body();
        String cookiesStr = rs.getCookieStr();
//        String content=HttpUtil.get(urlString);
//        System.out.println(content);
        Document doc = Jsoup.parse(content);
        Elements divs = doc.getElementsByClass("listmain");
//        Element tbody = queues.getElementsByTag("tbody").get(0);
        Elements as = divs.get(0).getElementsByTag("a");
        boolean isFind = false;
        FileWriter fw = new FileWriter("D:\\小说\\万道神帝李沉渊.txt");
        BufferedWriter bw = new BufferedWriter(fw);
        for (Element a : as) {
//            System.out.println(tr);
            String title = a.text();
            System.out.println(title);
            String href = a.attr("href");

            String url = "https://www.22mt.com" + href;
            String text= HttpHelper.get(url);
//            System.out.println(text);
//            System.out.println(text);
//            System.out.println();
            Document ct = Jsoup.parse(text);
            Element content1 = ct.getElementById("content");
            String xx= content1.toString();
            String x = xx.replaceAll("<div id=\"content\" class=\"showtxt\">", "")
                    .replaceAll("<br>", "")
                    .replaceAll("</div>", "")
                    .replaceAll("&nbsp;", "");
//            System.out.println(x);
            System.out.println();

            bw.write(title);
            bw.newLine();
            bw.write(x);
            bw.newLine();
            bw.flush();
        }
        bw.close();
        fw.close();
    }
}
